package map;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Demo5 {
    public static void main(String[] args) {
        HashMap<Student, String> map = new HashMap<>();
        Student s1 = new Student("张三",18);
        Student s2 = new Student("李四",28);
        Student s3= new Student("王老五",33);
        map.put(s1,"湖南");
        map.put(s2,"湖北");
        map.put(s3,"上海");
        //第一种遍历，通过所有的键对象
        Set<Student> keys = map.keySet();
        for (Student key : keys) {
            String value = map.get(key);
            System.out.println(key+"----"+value);
        }
        System.out.println("=====================");
        //第二种遍历，通过键对象
        Set<Map.Entry<Student, String>> entries = map.entrySet();
        for (Map.Entry<Student, String> entry : entries) {
            Student key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key+"-----"+value);
        }
        System.out.println("=====================");
        //第三种遍历，通过方法 forEach
        map.forEach(
                (Student key , String value )->{
                    System.out.println(key+"-----"+value);
                }
        );
    }
}
